﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace zielonka.co.uk.extensions.system
{
    //  Description:	Gets an enumeration of assembly attributes of the specified type from the assembly it is called from.
    /*
       var attributes = Assembly.GetExecutingAssembly().GetAttributes<InstallPerformanceCounterAttribute>();
     */

    /// <summary>
    /// Extension methods for dealing with an assembly
    /// </summary>
    public static class AssemblyExtensions
    {
        /// <summary>
        /// Gets the attributes from an assembly.
        /// </summary>
        /// <typeparam name="T">The type of the custom attribute to find.</typeparam>
        /// <param name="callingAssembly">The calling assembly to search.</param>
        /// <returns>An enumeration of attributes of type T that were found.</returns>
        public static IEnumerable<T> GetAttributes<T>(this Assembly callingAssembly)
            where T : Attribute
        {
            // Try to find the configuration attribute for the default logger if it exists
            object[] configAttributes = Attribute.GetCustomAttributes(callingAssembly,
                typeof(T), false);

            if (configAttributes != null)
            {
                foreach (T attribute in configAttributes)
                {
                    yield return attribute;
                }
            }
        }
    }

}
